package com.jarvis.user.service.impl;

import com.jarvis.user.dao.UserDao;
import com.jarvis.user.entity.UserEntity;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jarvis.common.utils.PageUtils;
import com.jarvis.common.utils.Query;

import com.jarvis.user.dao.UserRecordDao;
import com.jarvis.user.entity.UserRecordEntity;
import com.jarvis.user.service.UserRecordService;


@Service("userRecordService")
public class UserRecordServiceImpl extends ServiceImpl<UserRecordDao, UserRecordEntity> implements UserRecordService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserRecordEntity> page = this.page(
                new Query<UserRecordEntity>().getPage(params),
                new QueryWrapper<UserRecordEntity>()
        );

        return new PageUtils(page);
    }
    @Override
    public  PageUtils queryUserPage(Map<String, Object> params,String uid){
        QueryWrapper<UserRecordEntity> queryWrapper =  new QueryWrapper<UserRecordEntity>();
        queryWrapper.eq("user_id",uid);
        IPage<UserRecordEntity> page = this.page(
                new Query<UserRecordEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);

    }
    @Override
    public UserRecordEntity addRecord(UserRecordEntity entity){

        UserRecordDao recordDao = this.baseMapper;
        QueryWrapper<UserRecordEntity> queryWrapper =  new QueryWrapper<UserRecordEntity>();
        queryWrapper.eq("create_content",entity.getCreateContent());
        queryWrapper.eq("user_id",entity.getUserId());
        Integer count = Math.toIntExact(baseMapper.selectCount(queryWrapper));
        if(count==0) {
          recordDao.insert(entity);
        }

        return recordDao.selectOne(queryWrapper);


    }



}